package com.only4play.portal.cp.util.crypto;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.only4play.common.utils.Base64Utils;
import com.only4play.portal.cp.config.CpConfigStorage;

import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file PortalCpCryptUtil
 * @brief PortalCpCryptUtil
 * @details PortalCpCryptUtil
 * @date 2024-06-07
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-07               liyuncong          Created
 */
public class CpCryptUtil extends PortalCryptUtil {
    public CpCryptUtil(CpConfigStorage cpConfigStorage) {

    }

    public String encrypt(String data) {
        // todo 先暂时使用base进行加解密
        return Base64Utils.encode(data);
    }

    public String decrypt(String msgSignature, String timeStamp, String nonce, String encryptedData) {
        // todo 先暂时使用base进行加解密
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> stringObjectMap = objectMapper.readValue(encryptedData, new TypeReference<Map<String, Object>>() {
            });
            String content = (String) stringObjectMap.getOrDefault("content", "");
            String decode = Base64Utils.decode(content);
            stringObjectMap.put("content", decode);
            return objectMapper.writeValueAsString(stringObjectMap);

        } catch (Exception exception) {
            return "";
        }
    }
}
